{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#import import_ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "def encrypt_message(key: int, message: str) -> str:\n",
    "    \"\"\"\n",
    "    使用列置换加密消息\n",
    "    Args:\n",
    "        key: 加密密钥(列数)\n",
    "        message: 要加密的明文\n",
    "    Returns:\n",
    "        加密后的密文字符串\n",
    "    \"\"\"\n",
    "\n",
    "    # 创建列容器，列表元素为str，元素个数为key，\n",
    "    ciphertext_columns: List[str] = [''] * key\n",
    "    \n",
    "    # 填充各列，\n",
    "    # 通过外循环遍历各列\n",
    "    # 通过内循环以列为起点、key为步长读取明文\n",
    "    # 0,8,16,24,32,……\n",
    "    # 1,9,17,25,33,……\n",
    "    # 2,10,18,26,34,……\n",
    "    for column in range(key):\n",
    "        for current_index in range(column, len(message), key):\n",
    "            ciphertext_columns[column] += message[current_index]\n",
    "    \n",
    "    return ''.join(ciphertext_columns)\n",
    "    '''\n",
    "def encrypt_message(key: int, message: str) -> str:\n",
    "    ciphertext_columns = [''] * key\n",
    "    column = 0\n",
    "    for symbol in message:\n",
    "        ciphertext_columns[column] += symbol\n",
    "        column += 1\n",
    "        if column == key:\n",
    "            column = 0\n",
    "    return ''.join(ciphertext_columns)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
